package org.i_plus.model.services;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.hibernate.SessionFactory;
import org.i_plus.model.FriendBean;
import org.i_plus.model.MemberBean;
import org.i_plus.model.dao.FriendDAOJdbc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@Path("/friendService")
public class FriendService {

	private FriendDAOJdbc dao = null;
	public void setFriendDAOJdbc(FriendDAOJdbc dao){
		this.dao = dao;
	}
	
	public List<MemberBean> friendList(String fromacc){
		return dao.selectByFromacc(fromacc);
	}
	
	@POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes("application/json")
	public String addFriend(@QueryParam("fromacc")String fromacc,@QueryParam("toacc")String toacc){
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
		FriendDAOJdbc dao = (FriendDAOJdbc)context.getBean("FriendDAO");
    	SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
    	sessionFactory.getCurrentSession().beginTransaction();
		
    	String situation = ((FriendService)context.getBean("friendService")).checkSituation(fromacc, toacc);
		FriendBean bean = new FriendBean();
		if("對方要求加為好友".equals(situation)){//如果對方已要求 新增一筆並且修改舊有的 "表示已朋友"
			bean.setFromacc(fromacc);
			bean.setToacc(toacc);
			bean.setSituation("t");
			dao.insertFriend(bean);
			bean.setFromacc(toacc);
			bean.setToacc(fromacc);
			dao.update(bean);
		}else if("要求成為好友".equals(situation)){//如果雙方都沒關係 新增一筆 表示"已要求"
		bean.setFromacc(fromacc);
		bean.setToacc(toacc);
		bean.setSituation("w");
		dao.insertFriend(bean);
		}else if("已拒絕對方邀請".equals(situation)){//如果自己曾經拒絕 修改兩筆 表示"朋友"
			bean.setFromacc(fromacc);
			bean.setToacc(toacc);
			bean.setSituation("t");
			dao.update(bean);
			bean.setFromacc(toacc);
			bean.setToacc(fromacc);
			dao.update(bean);
			}
		String result = ((FriendService)context.getBean("friendService")).checkSituation(fromacc, toacc);
		sessionFactory.getCurrentSession().getTransaction().commit();
		return result;
	}
	
	//已是朋友 回傳"朋友" 已要求回傳to 對方要求from 完全沒關係回傳no 已拒絕回傳reject
	public String checkSituation(String fromacc ,String toacc){
		FriendBean bean = dao.select(fromacc, toacc);
		if(bean != null){
			String situation = bean.getSituation();
			if("t".equals(situation))
				return "朋友";
			if("w".equals(situation))
				return "已要求加為好友";
			if("n".equals(situation))
				return "已拒絕對方邀請";
		}else{
			bean = dao.select(toacc, fromacc);
			if(bean != null )
				return "對方要求加為好友";
		}
		return "要求成為好友";
	}
	
	@GET  
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes("application/json")
	public List<String> friendListRS(@QueryParam("fromacc")String fromacc){
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
		FriendDAOJdbc dao = (FriendDAOJdbc)context.getBean("FriendDAO");
    	SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
    	sessionFactory.getCurrentSession().beginTransaction();
 	
    	List<String >result = new ArrayList<String>();
		for(MemberBean bean:dao.selectByFromacc(fromacc))
			result.add(bean.getAccount());
 	
    	sessionFactory.getCurrentSession().getTransaction().commit();
		return result;
	}
	
	public static void main(String args[]){
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
		FriendService service = (FriendService)context.getBean("friendService");
    	SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
    	sessionFactory.getCurrentSession().beginTransaction();
		
    	System.out.println(service);
    	
   
    	
    	System.out.println(service.checkSituation("a19897811", "aa"));
    	System.out.println(service.checkSituation("a19897811", "bb"));
    	System.out.println(service.checkSituation("a19897811", "cc"));
    	System.out.println(service.checkSituation("a19897811", "dd"));
    	System.out.println(service.checkSituation("a19897811", "ee"));
    	System.out.println(service.checkSituation("a19897811", "q19897811"));
    	System.out.println(service.checkSituation("q19897811", "a19897811"));
    	
		sessionFactory.getCurrentSession().getTransaction().commit();
	
	}
}
